package com.servlet;

import com.bean.Student;
import com.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet("/ServletUpdate")
public class ServletUpdate extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("name");
        String stuid = request.getParameter("stuid");
        String stuname = request.getParameter("stuname");
        String stuage = request.getParameter("stuage");
        String stubirthday = request.getParameter("stubirthday");
        StudentService studentService = new StudentService();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = null;
        try {
            birthday = sim.parse(stubirthday);
        } catch (ParseException e){
            e.printStackTrace();
        }
        Student student = new Student(id, stuname, Integer.parseInt(stuage),birthday);
        request.setAttribute("student",student);
        Boolean b = studentService.update(student);
        if (b){
            response.sendRedirect("/ServletFind");
        }else {
            response.getWriter().write("修改失败");
            response.setHeader("Refresh","2;URL=/ServletFind");
        }
    }
}
